package main

import (
	"fmt"
	"godb/config"

	"github.com/gin-gonic/gin"
)

type Userq struct {
	Name string `json:"name"`
	Time int64  `json:"time"`
}

func main() {

	r := gin.Default()
	//新增
	r.GET("/add", func(c *gin.Context) {
		userq := Userq{Name: "Jinzhu", Time: 1234567891}
		result := config.GormDb.Table("user").Create(&userq)

		if result.Error != nil {
			fmt.Println("新增失败:", result.Error)
			return
		}
		// fmt.Println("user created:", user.ID)
	})
	//查询
	r.GET("/caxun", func(c *gin.Context) {
		var userq Userq
		result := config.GormDb.Table("user").Where("id = ?", 2).Take(&userq)

		if result.Error != nil {
			fmt.Println("查询失败:", result.Error)
			return
		}
		fmt.Println(userq)
	})

    r.GET("/caxuns", func(c *gin.Context) {
		var userq []Userq
		result := config.GormDb.Table("user").Where("name = ?", "Jinzhu").Find(&userq)

		if result.Error != nil {
			fmt.Println("查询失败:", result.Error)
			return
		}
		fmt.Println(userq)
	})
	//修改
	r.GET("/xougai", func(c *gin.Context) {
		result := config.GormDb.Exec("UPDATE user SET name = ?,time = ? WHERE id = ?", "xiaoming", 13, 1)
		if result.Error != nil {
			fmt.Println("修改失败:", result.Error)
			return
		}
	})
	//删除
	r.GET("/shancu", func(c *gin.Context) {
		result := config.GormDb.Exec("DELETE FROM user WHERE id = ?", 1)
		if result.Error != nil {
			fmt.Println("删除失败:", result.Error)
			return
		}
	})
	r.Run()
}
